﻿using FPSO.Models.Constant;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using static FPSO.Models.Constant.TankConstant;

namespace FPSO.HMI.Views.Dialog
{
    /// <summary>
    /// WBTTaskDialog.xaml 的交互逻辑
    /// </summary>
    public partial class WBTTaskDialog : Window
    {
        public WBTTaskDialog()
        {
            InitializeComponent();
            Result = MessageBoxResult.Cancel;

            List<ConstantTankInfo> taskInfos = TankConstant.getWBTTank();

            foreach (ConstantTankInfo taskInfo in taskInfos)
            {
                ComboBoxItem wbtItem = new ComboBoxItem() { Content = taskInfo.TankName, Tag = taskInfo };
                cmbWBT.Items.Add(wbtItem);
            }

            ComboBoxItem outType = new ComboBoxItem() { Content = TaskTypeConstant.UNLOAD_NAME };
            ComboBoxItem inType = new ComboBoxItem() { Content = TaskTypeConstant.LOAD_NAME };
            cmbTaskType.Items.Add(outType);
            cmbTaskType.Items.Add(inType);

            WBTId = "";
            WBTSpeed = 100;
            TaskType = TaskTypeConstant.LOAD_ID;
        }

        public MessageBoxResult Result { get; set; }

        public string WBTId { get; set; }
        public double WBTSpeed { get; set; }
        public int TaskType { get; set; }
        public double TargetVolume { get; set; }
		public double InitVolume { get; set; }
        private void Confirm_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                if (cmbWBT.SelectedItem == null)
                {
                    throw new Exception("请选择压载舱!");
                }
                if (cmbTaskType.SelectedItem == null)
                {
                    throw new Exception("请选择装载类型!");
                }

                double pipeSpeed;
                double.TryParse(txbWBTSpeed.Text, out pipeSpeed);
                if (pipeSpeed <= 0)
                {
                    throw new Exception("请输入正确的装卸速度!");
                }

                WBTSpeed = pipeSpeed;
                WBTId = ((ConstantTankInfo)((ComboBoxItem)cmbWBT.SelectedItem).Tag).TankID;
                TaskType = cmbTaskType.SelectedIndex == 0 ? TaskTypeConstant.UNLOAD_ID : TaskTypeConstant.LOAD_ID;
                TargetVolume = Convert.ToDouble(txbWBTVolume.Text);
                this.Result = MessageBoxResult.OK;
                this.Close();
            }
            catch(Exception ex)
            {
                MessageBox.Show(ex.Message, "错误", MessageBoxButton.OK, MessageBoxImage.Error);
            }
        }

        private void Cancel_Click(object sender, RoutedEventArgs e)
        {
            Result = MessageBoxResult.Cancel;
            this.Close();
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
			Window_reload();
		}

		public void Window_reload()
		{
			List<ConstantTankInfo> taskInfos = TankConstant.getWBTTank();
			if (WBTId != "")
			{
				for (int i = 0; i < taskInfos.Count; i++)
				{
					if (WBTId == taskInfos[i].TankID)
					{
						cmbWBT.SelectedIndex = i;
						break;
					}
				}
			}

			txbWBTSpeed.Text = WBTSpeed.ToString();

			if (TaskType == TaskTypeConstant.UNLOAD_ID)
			{
				cmbTaskType.SelectedIndex = 0;
			}
			else if (TaskType == TaskTypeConstant.LOAD_ID)
			{
				cmbTaskType.SelectedIndex = 1;
			}
			txbWBTVolume.Text = TargetVolume.ToString();
			txbWBTInitVolume.Text = InitVolume.ToString();
		}
		private void WindowMove(object sender, MouseEventArgs e)
		{
			if (e.LeftButton == MouseButtonState.Pressed)
			{
				DragMove();
			}
		}

	}
}
